﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Dynamic;
using System.Security.Policy;
using System.Text;
using System.Windows.Forms;
using CommonTools;
using CommonTools.Controls;
using WeifenLuo.WinFormsUI.Docking;
using Gtong;
using Gtong.BLL;
using Gtong.MODEL;
using Gtong.Properties;

namespace Gtong.Spare
{
	public partial class BeginDataForm : DockContent, IToolStripMergableForm, IDocForm<Gtong.MODEL.BeginData>
	{
		#region 属性, 字段

		/// <summary>
		/// 单据bll, 直接修改父类的内容
		/// </summary>
		public BLL.Spare.BeginDataBll DocBll { set; get; }

		/// <summary>
		/// 单据 直接修改父类的内容
		/// </summary>
		public BeginData Doc
		{
			set
			{
				this._document = value ?? new BeginData();
				this.LoadDoc();
			}
			get => this._document;
		}

		private BeginData _document;

		private bool _isEditing = false;

		private bool _DataInExcel = false;
		#endregion 属性, 字段

		#region 构造函数

		public BeginDataForm()
		{
			InitializeComponent();

			//单据的model, bll, 注意要先赋值bll,再赋值model
			this.DocBll = new BLL.Spare.BeginDataBll();
			this.Doc = new BeginData();

			#region 设置 Grid

			this.Grid.AutoGenerateColumns = false;   //禁止表格自动生成列

			//todo 限定列中值的类型
			//this.Grid.Columns[cl_Amount.Index].ValueType = typeof(decimal);
			//this.Grid.Columns[cl_Repayment.Index].ValueType = typeof(decimal);
			//this.Grid.Columns[cl_OccurDate.Index].ValueType = typeof(DateTime);
			this.cl_Fdate.ValueType = typeof(DateTime);
			this.cl_IsdelDate.ValueType = typeof(DateTime);
			
			//隐藏明细里部分列
			foreach (DataGridViewColumn item in this.Grid.Columns)
			{
				if (item.Name.StartsWith("cl_") == false)
					item.Visible = false;
			}

			#endregion 设置 Grid

			
			//检查权限
			RightTool.CheckRight(this);

			//更换用户事件,检查权限 
			UserBll.UserChanged += () => RightTool.CheckRight(this);

			//
			this.sta_Err.Text = "";
			this.sta_checked.Text = "";
			this.sta_LastTime.Text = "";
			this.sta_SelectionSum.Text = "";

			Cmb_DataTypeAddItems();
		}

		public BeginDataForm(IDoc doc)
			: this()
		{
			this.Doc = doc as BeginData;
		}

		#endregion 构造函数


		#region 实现IDocForm的接口: 新增,保存,创建窗口,显示窗口,

		/// <summary>
		/// 重载的保存方法, 会在实际保存前, 检查单据内容
		/// </summary>
		public bool Save()
		{
			//todo 不能绑定数据的,在这里赋值, combobox 控件绑定数据有问题, 只能手动赋值给单据Doc
			
			
			string err = "";

			if (this.DocBll.Save(this.Doc, UserBll.CurrentUser, out err) == false)
			{
				//保存失败
				MessageBox.Show(err, Properties.Resources.systemInformation, MessageBoxButtons.OK,
					MessageBoxIcon.Information);

				this.sta_Err.SetMessage(err, true);
				return false;
			}
			else
			{
				this.sta_Err.SetMessage("保存成功!", true);

				//将按钮的状态改成只读
				DocFormHelper.SetSaveStatus(SaveStatus.ReadOnly, this, toolSave, this.toolModify, toolSaveModify);

				MessageTip.ShowOk("保存成功!");

				//todo 如果不是EF方式保存的表体, 需要刷新表体数据,
				this.Doc.Items = this.DocBll.GetItems(this.Doc);

				//显示数据  这里都应该用绑定
				if (this.Doc.LastModifyTime != null)
					this.sta_LastTime.Text = this.Doc.LastModifyTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
				else
					this.sta_LastTime.Text = "";

				return true;
			}
		}

		/// <summary> 删除 </summary>
		public void Delete()
		{
			if (MessageBox.Show("确认要删除此单据吗?", Properties.Resources.systemInformation, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
				!= DialogResult.Yes)
			{
				return;
			}

			if (this.DocBll.Delete(this.Doc, UserBll.CurrentUser, out var err))
			{
				MessageTip.ShowOk("单据已删除!");
				this.Close();  //直接关闭窗体

				//hack 删除后 二级窗体也要删除右侧列表, 已经在二级窗体里改了功能了, 需要测试 
			}
			else
			{

				//删除失败
				MessageBox.Show(err, Properties.Resources.systemInformation, MessageBoxButtons.OK,
					MessageBoxIcon.Warning);

				this.sta_Err.SetMessage(err, true);
			}
		}

		private void LoadDoc()
		{

			//通过绑定数据的方式 加载单据内容
			this.docBindingSource.DataSource = new List<BeginData>() { this.Doc };
			if (this.Doc != null && this.Doc.DocId>0)
			{
				this.Doc.Items = this.DocBll.GetItems(this.Doc);   //表体明细

				if (this.Doc.LastModifyTime != null)
					this.sta_LastTime.Text = this.Doc.LastModifyTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
				else
					this.sta_LastTime.Text = "";

				//保存状态, 这里会影响保存按钮的显示
				DocFormHelper.SetSaveStatus((this.Doc.DocId > 0 ? SaveStatus.ReadOnly : SaveStatus.CanSave),
					this, this.toolSave, this.toolModify, this.toolSaveModify);
			}
			else
			{
				//控件赋值, 清空
				this.box_Date.Value = DateTime.Today;
				this.sta_LastTime.Text = "";
				//保存状态, 这里会影响保存按钮的显示
				DocFormHelper.SetSaveStatus(SaveStatus.CanSave, this, this.toolSave, this.toolModify, this.toolSaveModify);
			}
			
			this.sta_Err.SetMessage(); //清空提示
		}


		/// <summary> 创建新窗体, 但是不显示出来 </summary>
		public IDocForm<BeginData> CreateForm(IDoc doc)
		{
			return new BeginDataForm(doc);
		}

		#endregion 实现IDocForm的接口: 新增,保存,创建窗口,显示窗口,

		#region 上方菜单按钮

		/// <summary> 保存按钮, 实现三个状态切换: 保存,修改,保存修改 </summary>
		private void toolSave_Click(object sender, EventArgs e)
		{
			//直接保存
			this.Save();
		}

		/// <summary> 修改 </summary>
		private void toolModify_Click(object sender, EventArgs e)
		{
			if (toolSaveModify.Enabled)
			{
				//撤销
				//当前可以保存修改， 那么再点这个按钮就是撤销
				DocFormHelper.SetSaveStatus(SaveStatus.ReadOnly, this, this.toolSave, this.toolModify, this.toolSaveModify);

				//删除正在编辑记录
				BLL.EditingBll.Clear(Doc.DocFlag, this.Doc.DocId); 

			}
			else
			{
				// 这时, 这个按钮是显示的文字是"修改", 
				//检查权限 不是自己的单据需要额外的权限
				if (this.Doc.PreparedBy != UserBll.CurrentUser.Name
					&& BLL.UserBll.CheckRight(this.DocBll.ModifyPermission) == false)
				{
					MessageBox.Show("对不起，您没有权限修改他人单据！", Properties.Resources.systemInformation, MessageBoxButtons.OK,
						MessageBoxIcon.Warning);
					this.sta_Err.SetMessage("对不起，您没有权限修改他人单据！", true);
				}
				else
				{
					 //检查是否可以修改 
					var status = this.DocBll.GetStatus(this.Doc);
					if (status.EnableModify == false)
					{
						MessageBox.Show(status.Reason, Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Warning);
						this.sta_Err.SetMessage(status.Reason, true);
						return;
					}
				
					//添加正在编辑记录
					if (BLL.EditingBll.TryAdd(this.Doc, UserBll.CurrentUser, out var editing) == false)
					{
						string err = $"对不起, 当前单据正在被 {editing.UserName} 编辑，您不能修改！\r\n时间：{editing.EditTime: yyyy-M-d h:m:s}";
						MessageBox.Show(err, Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Warning);
						this.sta_Err.SetMessage(err, true);
					}
					else
					{
						//改变单据编辑状态
						DocFormHelper.SetSaveStatus(SaveStatus.CanModify, this, this.toolSave,this.toolModify, this.toolSaveModify);
						MessageTip.ShowOk(this, "可以编辑了!");
					}

				}

			}
		}

		/// <summary> 保存修改 </summary>
		private void toolSaveModify_Click(object sender, EventArgs e)
		{
			//直接保存修改
			this.Save();
		}



		private void toolRefresh_Click(object sender, EventArgs e)
		{
			if (this.Doc != null)
			{
				this.Doc = (BeginData)(this.DocBll.GetDoc(this.Doc.DocId));
			}
		}



		#region 打印

		/// <summary> 打印预览 </summary>
		private void toolPrintPreView_Click(object sender, EventArgs e)
		{
			if (this.Doc != null &&  this.Doc.DocId ==0 )
			{
				MessageBox.Show("请先保存后再打印", Resources.systemInformation, MessageBoxButtons.OK,
					MessageBoxIcon.Information);
				return;
			}

			this.PrintDoc(true); 
		}

		/// <summary> 打印 </summary>
		private void toolPrint_Click(object sender, EventArgs e)
		{
			if (this.Doc != null && this.Doc.DocId == 0)
			{
				MessageBox.Show("请先保存后再打印", Resources.systemInformation, MessageBoxButtons.OK,
					MessageBoxIcon.Information);
				return;
			}

			this.PrintDoc(false); 
		}

		/// <summary> 打印 </summary>
		/// <param name="isPreview">是否预览</param>
		private void PrintDoc(bool isPreview)
		{
			//GtPrintEditor.GtPrintDocument printFile = GtPrintDocument.FromFile("合同结算模板");
			//if (printFile == null)
			//{
			//    MessageBox.Show("找不到打印模板!", Resources.systemInformation, MessageBoxButtons.OK,
			//        MessageBoxIcon.Information);
			//    return;
			//}

			//// todo 参数按照模板进行调整 参数 
			//PrintParameter[] prs = new PrintParameter[]
			//{
			//    new PrintParameter("time"){ Value = DateTime.Now},
			//    new PrintParameter("打印次数"){ Value = this.Doc.PrintCount +1}, 
			//    new PrintParameter("doc"){ Value = this.Doc},
			//};

			//// todo 模板未实现这个代码 修改打印次数
			//// printFile.PrintSuccess += d =>  this.DocBll.AddPrintCount(this.Doc);      //PrintSuccess是打印成功事件

			//if (isPreview)
			//    printFile.PrintPreView(prs); //预览
			//else
			//    printFile.PrintChoosePrinter(prs);        //打印, 会提示先择打印机
		}

		#endregion


		/// <summary> 审核 </summary>
		private void toolCheck_Click(object sender, EventArgs e)
		{
			if (this.DocBll.CheckDocument(this.Doc, UserBll.CurrentUser, out var err) == true)
			{
				this.sta_Err.SetMessage();
				MessageTip.ShowOk(this, "单据已审核！");
			}
			else
			{
				MessageBox.Show(err, Properties.Resources.systemInformation, MessageBoxButtons.OK,
					MessageBoxIcon.Information);
				this.sta_Err.SetMessage(err, true);
			}
		}

		/// <summary> 反审核 </summary>
		private void toolUnCheck_Click(object sender, EventArgs e)
		{
			if (this.DocBll.UnCheckDocument(this.Doc, UserBll.CurrentUser, out var err) == true)
			{
				this.sta_Err.SetMessage();
				MessageTip.ShowOk(this, "单据已反审核！");
			}
			else
			{
				MessageBox.Show(err, Properties.Resources.systemInformation, MessageBoxButtons.OK,
					MessageBoxIcon.Information);
				this.sta_Err.SetMessage(err, true);
			}
		}

		private void toolDelete_Click(object sender, EventArgs e)
		{
			this.Delete();
		}

		#endregion 上方菜单按钮

		#region 其他按钮

		private void btn_newRow_Click(object sender, EventArgs e)
		{
			int c = this.box_AddRowCount.Value.IsNull(0);
			this.Grid.AddRows(c);
		}

		private void btn_showQueryBox_Click(object sender, EventArgs e)
		{
			if (this.ParentForm is DocDockxx dd)
			{
				dd.toolQuery.PerformClick();  //点击查询按钮
			}
		}

		#endregion 其他按钮

		#region 实现合并工具栏的接口

		/// <summary> 实现合并工具栏的接口 </summary>
		public ToolStrip MergedToolStrip => this.toolStrip1;

		#endregion 实现合并工具栏的接口

		#region 窗体事件

		/// <summary> 窗体关闭事件 </summary>
		private void Form_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.Grid.SaveWidthIni(this.GetType().Name); //记录列宽
			
			//如果当前正在编辑状态， 需要删除正在编辑记录
			if (this.toolSaveModify.Enabled)
				BLL.EditingBll.Clear(  Doc.DocFlag, this.Doc.DocId); 
		}

		private void Form_Load(object sender, EventArgs e)
		{
			this.Grid.ReLoadWidthIni(this.GetType().Name);  //从记录中载入列宽

		   
		}


		#endregion 窗体事件

		#region DataGridView 控件事件

		private void Grid_SelectionChanged(object sender, EventArgs e)
		{
			this.sta_SelectionSum.Text = this.Grid.SelectionSumString;
		}

		private void Grid_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			if (e != null && e.Exception != null)
			{
				if (e.Exception is System.ArgumentException)
				{   //combobox 错误, 输入的值不在列表中, 
					e.Cancel = true;
				}
				else
				{
					MessageBox.Show(e.Exception.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
					this.sta_Err.SetMessage(e.Exception.Message, true);
					e.Cancel = true;
				}
			   
			}
		}

		private void Grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex<0 || e.RowIndex <0 )
				return;
			if (_isEditing)
				return;

			_isEditing = true;   //一下的代码不要用return , 否则_isEditing不能还原
			if (_DataInExcel == false)
			{
				if (e.ColumnIndex == cl_Name.Index)
				{
					GetGoods(e.RowIndex, this.Grid[cl_Name.Index, e.RowIndex].Value.IsNull(""), false);
				}
				if (e.ColumnIndex == cl_Size.Index)
				{
					GetGoods(e.RowIndex, this.Grid[cl_Size.Index, e.RowIndex].Value.IsNull(""), false);
				}
				if (e.ColumnIndex == cl_StockPlace.Index)
				{
					GetStockPlace(e.RowIndex, this.Grid[cl_StockPlace.Index, e.RowIndex].Value.IsNull(""), false);
				}
				if (e.ColumnIndex == cl_StockPlaceID.Index)
				{
					GetStockPlace(e.RowIndex, this.Grid[cl_StockPlaceID.Index, e.RowIndex].Value.IsNull(""), false);
				}
				if (e.ColumnIndex == cl_Stock.Index)
				{
					GetStock(e.RowIndex, this.Grid[cl_Stock.Index, e.RowIndex].Value.IsNull(""), false);
				}
				if (e.ColumnIndex == cl_StockID.Index)
				{
					GetStock(e.RowIndex, this.Grid[cl_StockID.Index, e.RowIndex].Value.IsNull(""), false);
				}
			}
			_isEditing = false;
		}

		/// <summary> 自动填入默认值 </summary>
		private void Grid_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
		{
			if (e.Row.Index <0 )
			{
				return;
			}

			//e.Row.Cells[cl_dw.Index].Value = "吨";
		}

		/// <summary> 快捷键 </summary>
		private void Grid_KeyUp(object sender, KeyEventArgs e)
		{
			if (this.Grid.Rows.Count ==0)
			{
				return;
			}

			if (e.KeyCode == Keys.F7)
			{
				int r = this.Grid.SelectedCells[0].RowIndex;

				if (this.Grid.SelectedCells[0].ColumnIndex == cl_Name.Index)
				{
					GetGoods(r, this.Grid[cl_Name.Index, r].Value.IsNull(""), true);
				}
				if (this.Grid.SelectedCells[0].ColumnIndex == cl_Size.Index)
				{
					GetGoods(r, this.Grid[cl_Size.Index, r].Value.IsNull(""), true);
				}
				if (this.Grid.SelectedCells[0].ColumnIndex == cl_Name.Index)
				{
					GetStockPlace(r, this.Grid[cl_StockPlace.Index, r].Value.IsNull(""), true);
				}
				if (this.Grid.SelectedCells[0].ColumnIndex == cl_Size.Index)
				{
					GetStockPlace(r, this.Grid[cl_StockPlaceID.Index, r].Value.IsNull(""), true);
				}
				if (this.Grid.SelectedCells[0].ColumnIndex == cl_Stock.Index)
				{
					GetStock(r, this.Grid[cl_Stock.Index, r].Value.IsNull(""), true);
				}
				if (this.Grid.SelectedCells[0].ColumnIndex == cl_StockID.Index)
				{
					GetStock(r, this.Grid[cl_StockID.Index, r].Value.IsNull(""), true);
				}

			}



		}

		#endregion DataGridView 控件事件

		private void Cmb_DataTypeAddItems()
		{
			Cmb_DataType.Items.Clear();
			Cmb_DataType.Items.Add("配件库存");
			Cmb_DataType.Items.Add("产品库存");
			Cmb_DataType.Items.Add("配件供应商余额");
			Cmb_DataType.Items.Add("原料供应商余额");
			Cmb_DataType.Items.Add("销售余额");
		}

		private void Cmb_DataType_SelectedIndexChanged(object sender, EventArgs e)
		{
			switch (Cmb_DataType.Text)
			{
				case "配件库存":
					Doc.DocType = "Equipment";
					break;
				case "产品库存":
					Doc.DocType = "Product";
					break;
				case "配件供应商余额":
					Doc.DocType = "EquipmentSupplier";
					break;
				case "原料供应商余额":
					Doc.DocType = "MaterialsSupplier";
					break;
				case "销售余额":
					Doc.DocType = "CustomersSurplus";
					break;
				default:
					break;
			}
		}
		/// <summary>
		/// 导入数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Btn_InExcel_Click(object sender, EventArgs e)
		{

			DataTable dt = ExcelHelper.Import();
			if (dt==null || dt.Rows.Count<=0)
			{
				MessageTip.Show(this, "数据导入失败，请检查");
				return;
			}
			DatatableToItems(dt);
			
		}
		private void DatatableToItems(DataTable dt)
		{
			Doc = null;
			_DataInExcel = false;
			List<BeginDataItem> list = new List<BeginDataItem>();
			//foreach (DataRow row in dt.Rows)
			//{
			//    BeginDataItem item = new BeginDataItem();
			//    foreach (DataColumn col in dt.Columns)
			//    {
			//        string s = row[col].ToString();
			//    }
			//    list.Add(item);
			//}
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				BeginDataItem item = new BeginDataItem();

				if (dt.Rows[i][0].ToString().Trim() != "")
				{
					try
					{
						item.Id = 0;
						item.DocId = 0;
						item.DocType = dt.Rows[i][0].ToString();
						item.Fdate = Convert.ToDateTime(dt.Rows[i][1].ToString());
						item.Fmonth = Convert.ToInt32(dt.Rows[i][2].ToString());
						item.Code = dt.Rows[i][3].ToString();
						item.Name = dt.Rows[i][4].ToString();
						item.Size = dt.Rows[i][5].ToString();
						item.Unit = dt.Rows[i][6].ToString();
						item.StockID = dt.Rows[i][7].ToString();
						item.Stock = dt.Rows[i][8].ToString();
						item.StockPlaceID = dt.Rows[i][9].ToString();
						item.StockPlace = dt.Rows[i][10].ToString();
						item.Qty = Convert.ToDecimal(dt.Rows[i][11].ToString());
						item.ReadyMoney = Convert.ToDecimal(dt.Rows[i][12].ToString());
						item.BigAcceptance = Convert.ToDecimal(dt.Rows[i][13].ToString());
						item.SmallAcceptance = Convert.ToDecimal(dt.Rows[i][14].ToString());
						item.Dollar = Convert.ToDecimal(dt.Rows[i][15].ToString());
						item.TotalMoney = Convert.ToDecimal(dt.Rows[i][16].ToString());
						item.InvoiceMoney = Convert.ToDecimal(dt.Rows[i][17].ToString());
						item.RemainQty = Convert.ToDecimal(dt.Rows[i][18].ToString());
					item.ReadyMoney = Convert.ToDecimal(dt.Rows[i][19].ToString());
						item.CautionMoney = Convert.ToDecimal(dt.Rows[i][20].ToString());
						item.Remarks = dt.Rows[i][21].ToString();
						item.Isdel = false;
						item.IsdelDate = null;

						list.Add(item);
					}
					catch (Exception ex)
					{
						throw new Exception(ex.Message);
					}
				   
				}
			}
			Grid.AddRows(list.Count);

			Doc.Items.Clear();
			Doc.Items.AddRange(list);
			MessageTip.Show(this, "数据导入成功,请保存");
			_DataInExcel = true;
		}

		/// <summary>
		/// 单据体输入物料名称 调取物料基础信息
		/// </summary>
		/// <param name="rowIndex"></param>
		/// <param name="arg2"></param>
		private void GetGoods(int rowIndex, string arg2, bool isKey)
		{
			if (string.IsNullOrEmpty(arg2) && !isKey) //内容空的, 直接退出 
			{
				//将单据内容关联属性清空 
				Grid.Rows[rowIndex].Cells[cl_Code.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_Name.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_Size.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_Unit.Index].Value = "";
				return;
			}

			JMateriel Materiel = FastBox.GetValue<JMateriel, BLL.BasicInfo.JMaterielBll>(arg2, isKey);

			if (Materiel != null)
			{
				Grid.Rows[rowIndex].Cells[cl_Code.Index].Value = Materiel.Code;
				Grid.Rows[rowIndex].Cells[cl_Name.Index].Value = Materiel.Name;
				Grid.Rows[rowIndex].Cells[cl_Size.Index].Value = Materiel.Model;
				Grid.Rows[rowIndex].Cells[cl_Unit.Index].Value = Materiel.Unit;

				return;
			}
			else
			{
				Grid.Rows[rowIndex].Cells[cl_Code.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_Name.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_Size.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_Unit.Index].Value = "";
				return;
			}
		}
		/// <summary>
		/// 单据体调取库区辅助资料信息
		/// </summary>
		/// <param name="rowIndex"></param>
		/// <param name="arg2"></param>
		private void GetStockPlace(int rowIndex, string arg2, bool isKey)
		{
			if (string.IsNullOrEmpty(arg2) && !isKey) //内容空的, 直接退出 
			{
				//将单据内容关联属性清空 
				Grid.Rows[rowIndex].Cells[cl_StockPlace.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_StockPlaceID.Index].Value = "";
				return;
			}

			JSubMessage subMessage = FastBox.GetValue<JSubMessage, BLL.BasicInfo.JSubMessageBll>(arg2, false, "9");

			if (subMessage != null)
			{
				Grid.Rows[rowIndex].Cells[cl_StockPlaceID.Index].Value = subMessage.Code;
				Grid.Rows[rowIndex].Cells[cl_StockPlace.Index].Value = subMessage.Name;
				return;
			}
			else
			{
				Grid.Rows[rowIndex].Cells[cl_StockPlace.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_StockPlaceID.Index].Value = "";
				return;
			}
		}
		////仓库
		private void GetStock(int rowIndex, string arg2, bool isKey)
		{
			if (string.IsNullOrEmpty(arg2) && !isKey) //内容空的, 直接退出 
			{
				//将单据内容关联属性清空 
				Grid.Rows[rowIndex].Cells[cl_Stock.Index].Value = "";
				Grid.Rows[rowIndex].Cells[cl_StockID.Index].Value = "";
				return;
			}
			if (Cmb_DataType.Text.Length>0)
			{
				switch (Cmb_DataType.Text)
				{
				//Cmb_DataType.Items.Add("产品库存");
				//Cmb_DataType.Items.Add("配件供应商余额");
				//Cmb_DataType.Items.Add("原料供应商余额");
				//Cmb_DataType.Items.Add("销售余额");
					case "配件库存":
						JStock Stock = FastBox.GetValue<JStock, BLL.BasicInfo.JStockBll>(arg2, isKey);
						if (Stock != null)
						{
							Grid.Rows[rowIndex].Cells[cl_Stock.Index].Value = Stock.Code;
							Grid.Rows[rowIndex].Cells[cl_StockID.Index].Value = Stock.Name;
							return;
						}
						else
						{
							Grid.Rows[rowIndex].Cells[cl_Stock.Index].Value = "";
							Grid.Rows[rowIndex].Cells[cl_StockID.Index].Value = "";
							return;
						}
					case "配件供应商余额":
						JSupplier Supplier = FastBox.GetValue<JSupplier, BLL.BasicInfo.JSupplierBll>(arg2, isKey);
						if (Supplier != null)
						{
							Grid.Rows[rowIndex].Cells[cl_Stock.Index].Value = Supplier.Code;
							Grid.Rows[rowIndex].Cells[cl_StockID.Index].Value = Supplier.Name;
							return;
						}
						else
						{
							Grid.Rows[rowIndex].Cells[cl_Stock.Index].Value = "";
							Grid.Rows[rowIndex].Cells[cl_StockID.Index].Value = "";
							return;
						}
					default:
						break;
				}
			}
			

		   
		}

	}
	
}


 
